import {
    defineStore
} from 'pinia'
import {
    computed,
    reactive,
    ref
} from 'vue'

export const useAppStore = defineStore('app', () => {
    // 当前主题
    const theme = ref('light')
    // 主题切换
    const change = () => {
        if (theme.value == 'light') {
            theme.value = 'dark'
            return
        }
        theme.value = 'light'
    }
    // 当前图标
    const icon = computed(() => {
        if (theme.value == 'light') {
            return '/static/dark-theme.svg'
        }
        return '/static/light-theme.svg'
    })
    // 页面数据
    const data = reactive({
        list: {},
        other: {},
    })
    // 当前标签栏
    const tab = ref(0)

    // 表单数据
    const formData = reactive({
        status: false,
        firstOrder: '',
		tactics: '',
        allAddPositionNum: '',
        profitRatio: '',
        profitCallbackRatio: '',
        stopLossRatio: '',
        addPositionCallbackRatio: '',
        subTitle: '',
        usdtInit: '',
        password: ''
    });

    // 更新表单数据
    const updateFormData = () => {
        formData.status = !data.other.status
        formData.firstOrder = data.other.firstOrder
        formData.tactics = data.other.tactics
        formData.allAddPositionNum = data.other.allAddPositionNum
        formData.profitRatio = data.other.profitRatio
        formData.profitCallbackRatio = data.other.profitCallbackRatio
        formData.stopLossRatio = data.other.stopLossRatio
        formData.addPositionCallbackRatio = data.other.addPositionCallbackRatio
        formData.subTitle = data.other.subTitle
        formData.usdtInit = data.other.usdtInit
    }

    return {
        theme,
        change,
        data,
        tab,
        icon,
        formData,
        updateFormData,
    }
}, {
    // store持久化
    persist: {
        key: "store",
        // 只持久化必要数据，免得刷新页面后读到了缓存数据，还以为没亏钱~^_^~
        paths: ['theme', 'tab', 'icon'],
    }
})